package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"structplugin"

	"github.com/go-redis/redis/v8"
)

func mustGetRedisClient(ctx context.Context) *redis.Client {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379", // Redis 地址
		Password: "",               // 密码（如果设置）
		DB:       0,                // 使用默认 DB
	})

	_, err := rdb.Ping(ctx).Result()
	if err != nil {
		panic(err)
	}

	return rdb

}

type Target struct {
	Uint64    uint64
	Uint32    *uint32
	Int       int
	Int64     int64
	Int64Ptr  *int64
	Float32   float32
	FloatPtr  *float64
	String    string
	StringPtr *string
	TimePtr   *time.Time
	Time      time.Time
}

func main() {

	ctx := context.Background()
	client := mustGetRedisClient(ctx)

	uint32Val := uint32(32)
	int64Val := int64(1000)
	float64Val := float64(3.1415926)
	strVal := "hello world"
	now := time.Now()

	t1 := Target{
		Uint64:    6666,
		Uint32:    &uint32Val,
		Int:       99,
		Int64:     int64Val,
		Int64Ptr:  &int64Val,
		Float32:   3.1415927,
		FloatPtr:  &float64Val,
		String:    strVal,
		StringPtr: &strVal,
		TimePtr:   &now,
		Time:      now,
	}

	values, err := structplugin.Marshal(t1)
	if err != nil {
		panic(err)
	}

	fmt.Println(values)

	key := "for_test"

	err = client.HSet(ctx, key, values).Err()
	if err != nil {
		panic(err)
	}

	retmap, err := client.HGetAll(ctx, key).Result()
	if err != nil {
		panic(err)
	}

	fmt.Println("ret", retmap)

	rets, _ := json.MarshalIndent(retmap, "", "  ")
	fmt.Println(string(rets))

	var t2 Target
	err = structplugin.Unmarshal(retmap, &t2)
	if err != nil {
		panic(err)
	}
	fmt.Println("after parse: ", t2)

	js, _ := json.MarshalIndent(t2, " ", "")
	fmt.Println(string(js))

	fmt.Println(string(rets))
}


